package cc.eddic.examinationsystem.web;



import cc.eddic.examinationsystem.core.BaseController;
import cc.eddic.examinationsystem.domain.Exam;
import cc.eddic.examinationsystem.domain.ExamQuestion;

import cc.eddic.examinationsystem.domain.Student;
import cc.eddic.examinationsystem.repository.*;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;


@Slf4j
@Controller
@RequestMapping("/answerbook")
public class AnswerController  extends BaseController {

    public AnswerController(ExamQuestionRepository examQuestionRepository, ExamRepository examRepository,  StudentRepository studentRepository) {
        this.examQuestionRepository = examQuestionRepository;
        this.examRepository = examRepository;
        this.studentRepository = studentRepository;
    }

    @GetMapping("/exam/{examId}/student/{studentId}")
    public String list(@PathVariable("examId") Long examId,@PathVariable("studentId") Long studentId,Model model)  {
        Exam exam=examRepository.findById(examId).orElseThrow(NoSuchElementException::new);
        Student student=studentRepository.findById(studentId).orElseThrow(NoSuchElementException::new);
        model.addAttribute("exam",exam);
        model.addAttribute("student",student);
        List<ExamQuestion> examQuestions = exam.getQuestions().stream()
                .filter(question -> question.getExam().getId() == examId)
                .collect(Collectors.toList());
        model.addAttribute("examQuestions",examQuestions);
        return "answerbook";
    }




    private final ExamQuestionRepository examQuestionRepository;
    private final ExamRepository examRepository;
    private final StudentRepository studentRepository;





}


